package br.unifor.cct.mia.runner;

import java.io.File;
import java.io.IOException;

import weka.gui.explorer.ClassifierPanel;
import br.unifor.cct.mia.coevolution.InvalidTypeException;
import br.unifor.cct.mia.dataenhancement.Database ;
import br.unifor.cct.mia.dataenhancement.GenotypeConverter;
import br.unifor.cct.mia.dataenhancement.Structure;
import br.unifor.cct.mia.evaluate.Evaluate;
import br.unifor.cct.mia.evaluate.classification.WekaClassification;
import br.unifor.cct.mia.evolutionary.Genotype;
import br.unifor.cct.mia.evolutionary.SpeciesConstants ;
import br.unifor.cct.mia.ga.GaIS;
import br.unifor.cct.mia.util.LoadFile;

public class InstanceSelection {

    public static String BASE_NAME = "soybean";
    public static String DATASET_STRUCTURE = "./data/"+BASE_NAME+"_estrutura.txt";
    public static String DATASET_DATA = "./dataPart/"+BASE_NAME+"_TRAIN_data.txt";
    public static String DATASET_TESTE = "./dataPart/"+BASE_NAME+"_TEST_data.txt";    

	public static Integer learnerType = Evaluate.J48;
	
    public static void main(String[] a) {    
        Structure st = LoadFile.loadStructure(DATASET_STRUCTURE);
        Database db = LoadFile.loadDatabase(DATASET_DATA);
        System.out.println("Arquivos carregados com sucesso");        
        
        try {            
            int nExecucoes = 10;
            int nBest[] = new int[nExecucoes];
            double nFitness[] = new double[nExecucoes];
            double nTrain[] = new double[nExecucoes];
            
            
            for (int i=0; i<nExecucoes; i++) {
                GaIS ga = new GaIS(db,st,null);
                Thread t = new Thread(ga);
                t.start();
                t.join();
                
                Genotype gen = (Genotype)ga.getBestIndividual();
                GenotypeConverter converter = new GenotypeConverter();
                
                nBest[i] = ga.getGenerationOfBest();
                nFitness[i] = gen.getFitness();
                
                try {
                    File trainFile = converter.convert(gen,SpeciesConstants.INSTANCE_SELECTION,"trainFile"+i+".txt",st,db,null,null);
                    File testFile = converter.convert(gen,SpeciesConstants.INSTANCE_SELECTION,"testeFile"+i+".txt",st,db,null,null);
                    converter.addTestData(gen,SpeciesConstants.INSTANCE_SELECTION,st,DATASET_TESTE,testFile);
                    
                    WekaClassification classifier = new WekaClassification(learnerType,null);
                    //classifier.setTestType(ClassifierPanel.SUPPLIED_TEST_SET);
                    nTrain[i] = classifier.evaluate(trainFile,trainFile);
                    
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InvalidTypeException e) {
                    e.printStackTrace();
                }                
            }
            
            for (int i=0; i<nExecucoes; i++){
                System.out.println("["+i+"]"+nBest[i]);
                System.out.println("["+i+"]"+nFitness[i]);
                System.out.println("["+i+"]"+nTrain[i]);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}